package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.pojo.Order;
import com.ssm.pojo.Product;
import com.ssm.pojo.ProductType;
import com.ssm.service.OrderService;
import com.ssm.service.ProductService;
import com.ssm.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/list")
    public String list(){
        return "product/product";
    }

    @RequestMapping("/listdata")
    @ResponseBody
    public Map<String,Object> listData(Integer page, Integer limit, String customerNO){
        List<Product> products = productService.selectProductsPage(page,limit,customerNO);
        Integer count = productService.countSize(customerNO);

        // 返回表格数据
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count",count);
        resultMap.put("data",products);
        return resultMap;
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String to_add(Model model){
        List<ProductType> productTypes = productTypeService.selectProductTypesPage(null,null,null);
        model.addAttribute("productTypes",productTypes);
        return "product/product_new";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(Product product){
        productService.insert(product);
        return "product/product";
    }

    @RequestMapping(value = "update",method = RequestMethod.GET)
    public String to_update(Model model,String productNO){
        List<ProductType> productTypes = productTypeService.selectProductTypesPage(null,null,null);
        model.addAttribute("productTypes",productTypes);

        Product product = productService.selectProductByNO(productNO);
        model.addAttribute("product",product);
        return "product/product_update";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(Product product){
        productService.update(product);
        return "product/product";
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Map<String,String> delete(String productNO){
        productService.delete(productNO);
        Map<String,String> map = new HashMap<String, String>();
        map.put("status","true");
        map.put("msg","删除成功");
        return map;
    }

    @RequestMapping(value = "inquiry",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> inquiry(HttpSession session,String productNO, Integer quantity, String otherinfo){
        Order order = new Order();
        order.setOrderNO(System.currentTimeMillis()+"");
        order.setProductNO(productNO);
        Customer customer = (Customer) session.getAttribute("customer");
        order.setCustomerNO(customer.getCustomerNO());
        order.setOrderTime(new Date());
        order.setQuantity(quantity);
        order.setStatus((short)0);

        // 计算总价
        Product product = productService.selectProductByNO(productNO);
        Double price = product.getPrice();
        BigDecimal sumprice = BigDecimal.valueOf(quantity*price);
        BigDecimal discount = new BigDecimal(1);
        // 确认折扣
        if(customer.getRank()==1){
            discount = new BigDecimal(0.9);
        }else if(customer.getRank()==2){
            discount = new BigDecimal(0.8);
        }else if(customer.getRank()==3){
            discount = new BigDecimal(0.7);
        }
        BigDecimal finalPrice = sumprice.multiply(discount);
        order.setQuoteprice(finalPrice);
        order.setOtherInfo(otherinfo);
        orderService.insert(order);

        Map<String,String> map = new HashMap<String, String>();
        map.put("status","true");
        map.put("msg","操作成功");
        return map;
    }
}
